package game

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

type ErrorMsg struct {
	Uid        string `p:"uid"  v:"required"  orm:"uid"`
	DeviceId   string `p:"deviceId"  v:"required"  orm:"device_id"`
	Version    string `p:"version"  v:"required"  orm:"version"`
	AppVersion string `p:"appVersion"  v:"required"  orm:"app_version"`
	OsVersion  string `p:"osVersion"  v:"required"  orm:"os_version"`
	Str        string `p:"str"  v:"required"  orm:"err_msg"`
	Channel    string `p:"channel"  v:"required"  orm:"channel"`
}

// path: /api/v1/upload_msg
func UploadMsg(r *ghttp.Request) {
	msg := new(ErrorMsg)
	if err := r.Parse(msg); err != nil {
		r.Response.WriteJsonExit(g.Map{
			"code": -1,
			"msg":  err.Error(),
		})
		return
	}

	_, err := g.DB().Table("game_upload_msg").Data(msg).Insert()
	if err != nil {
		r.Response.WriteJsonExit(g.Map{
			"code": -1,
			"msg":  err.Error(),
		})
	}

	r.Response.WriteJsonExit(g.Map{
		"code": 0,
		"msg":  "success",
	})
}

func GetMsg(r *ghttp.Request) {
	page := gconv.Int(r.Get("page", 1))
	pagesize := gconv.Int(r.Get("pagesize", 20))
	msg, err := g.DB().Table("game_upload_msg").Where("uid", r.Get("uid")).Limit((page-1)*pagesize, pagesize).Select()
	if err != nil {
		r.Response.WriteJsonExit(g.Map{
			"code": -1,
			"msg":  err.Error(),
		})
	}
	r.Response.WriteJsonExit(g.Map{
		"code": 0,
		"msg":  "success",
		"data": msg,
	})
}
